package com.bookcode.controller;

import com.bookcode.dao.PersonRepository;
import com.bookcode.entity.Person;
import org.reactivestreams.Publisher;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;



@RestController

public class PersonController {

    private final PersonRepository personRepository;

    public PersonController(PersonRepository personRepository) {
        this.personRepository = personRepository;
    }


    // 正常 MVC 模式
    @GetMapping("/")
    public String hello(){
        return "hello!";
    }

    //新增一个 Person

    @PostMapping("/person")
    public Mono<Void> add(@RequestBody Publisher<Person> person){
        return personRepository.insert(person).then();
    }


    @GetMapping("/person/{id}")
    public Mono<Person> getById(@PathVariable Long id){
        return personRepository.findById(id);
    }


    @GetMapping("/person/list")
    public Flux<Person> list(){
        return personRepository.findAll();
    }




    @DeleteMapping("/person/{id}")
    public Mono<Void> delete(@PathVariable Long id){
        return personRepository.deleteById(id).then();
    }

}

